<template>
  <div class="order">
    <div class="order__price">实付金额: &nbsp;<b>&yen;{{calculations.price}}</b></div>
    <div class="order__btn" @click="()=>toggleConfirmVisible(true)">提交订单</div>
  </div>

  <div
    class="mask"
    v-show="confirmVisible"
    @click="()=>toggleConfirmVisible(false)"
  >
    <div class="mask__content" @click.stop>
      <h3 class="mask__content__title">确认离开收银台吗?</h3>
      <p class="mask__content__desc">请尽快完成支付, 否则将被取消</p>
      <div class="mask__content__btn_box">
        <div class="mask__content__btn_box__btn"
             @click="()=>handleConfirmOrder(true)">取消订单</div>
        <div class="mask__content__btn_box__btn"
             @click="()=>handleConfirmOrder(false)">确认支付</div>
      </div>
    </div>
  </div>
</template>

<script>
import {useRoute, useRouter} from "vue-router";
import {useStore} from 'vuex';
import {ref} from "vue";

import {useCommonCartEffect} from "../../effects/cartEffect";
import {post} from "../../utils/request";

//下单相关
const useMakeOrderEffect = (shopId,shopName,productList) => {
  const router = useRouter();
  const store = useStore();

  const handleConfirmOrder = async (isCanceled)=>{
    //console.log('productList:',productList);

    const products = [];

    for (let i in productList.value){
      const product = productList.value[i]
      products.push({
        id: parseInt(product._id,10),
        num: product.count
      })
    }
    //console.log('handleConfirmOrder products:',products)

    try {
      const result = await post('/api/order', {
        addressId: 1,
        shopId,
        shopName: shopName.value,
        isCanceled,
        products
      })

      console.log('result:', result)
      if (result?.errno === 0) {
        //todo
        store.commit('clearCartData', shopId);
        router.push({name: 'OrderList'});
      }
    } catch (e) {
      //todo 提示下单失败
    }
  }

  return {handleConfirmOrder}
};

//弹框和蒙层
const useMaskEffect = ()=>{
  const confirmVisible = ref(false);
  const toggleConfirmVisible = (status) => {
    confirmVisible.value = status;
  }

  return {confirmVisible,toggleConfirmVisible}
}

export default {
  name: "Order",
  setup(){
    const route = useRoute();
    const shopId = parseInt(route.params.id,10);

    const {confirmVisible,toggleConfirmVisible} = useMaskEffect()
    const { calculations,shopName,productList } = useCommonCartEffect(shopId);
    const {handleConfirmOrder} = useMakeOrderEffect(shopId,shopName,productList)


    return { calculations,handleConfirmOrder,confirmVisible,toggleConfirmVisible }
  }
}
</script>

<style lang="scss" scoped>
@import '../../style/variables';

.order {
  display: flex;
  height: .49rem;
  line-height: .49rem;
  background: $bgColor;

  position: absolute;
  bottom: 0;
  left:0;
  right:0;

  &__price {
    flex:1;
    text-indent: .24rem;
    font-size: .14rem;
    color: $content-fontColor;
  }

  &__btn {
    width: .98rem;
    background: #4FB0F9;
    color: $bgColor;
    text-align: center;
    font-size: .14rem;
  }
}

.mask {
  z-index: 1;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background: rgba(0,0,0,.5);

  &__content{
    width: 3rem;
    height: 1.56rem;
    background: #FFFFFF;
    border-radius: .04rem;

    position:absolute;
    top:50%;
    left:50%;
    transform: translate(-50%, -50%);
    text-align: center;

    &__title {
      font-size: .18rem;
      margin: .24rem 0 0 0;
      line-height: .26rem;
      color: $content-fontColor;
    }
    &__desc {
      font-size: .14rem;
      color: $medium-fontColor;
      margin: .08rem 0 0 0;
    }
    &__btn_box {
      margin: .24rem .58rem;
      display: flex;
      &__btn {
        flex: 1;
        width: .8rem;
        line-height: .32rem;
        border-radius: .16rem;
        font-size: .14rem;

        &:first-child{
          margin-right: .12rem;
          border: .01rem solid #4FB0F9;
          color: #4FB0F9;
        }
        &:nth-of-type(2){
          margin-left: .12rem;
          background: #4FB0F9;
          color: #FFFFFF;
        }
      }
    }
  }
}
</style>
